package mhl.view;

import mhl.domain.*;
import mhl.service.*;
import mhl.utils.Utility;

import java.util.Date;
import java.util.List;

/**
 * @author DCSGO
 * @version 1.0
 * 满汉楼各个交互界面(菜单)
 */
public class MHLView {
    private boolean loop = true;
    private String input;
    private final EmployeeService employeeService = new EmployeeService();
    private final DiningTableService diningTableService = new DiningTableService();
    private final MenuService menuService = new MenuService();
    private final BillService billService = new BillService();
    private final MultiTableBeanService multiTableBeanService = new MultiTableBeanService();

    public static void main(String[] args) {
        new MHLView().mainMenu();
    }

    public void checkout() {
        System.out.println("===============结账===============");
        System.out.print("请选择要结账的餐桌编号(0退出): ");
        int dinId = Utility.readInt();
        if (dinId == 0) {
            System.out.println("已退出结账...");
            return;
        }
        if (!billService.hasUncheckoutBill(dinId)) {
            System.out.println("没有该餐桌或该餐桌没有需要结账的账单！");
            return;
        }
        System.out.println("请输入结账的方式(现金/支付宝/微信)回车表示退出：");
        String state = Utility.readString(3, "exit");
        if (state.equals("exit")) {
            System.out.println("已退出结账...");
            return;
        }
        if (billService.updateStateByDinId(dinId, state)) {
            System.out.println("结账成功！");
        } else {
            System.out.println("结账失败！");
        }
    }

    public void showAllBill() {
        List<Bill> allBill = billService.getAllBill();
        System.out.println("编号\t\t菜品编号\t数量\t\t合计\t\t\t桌号\t\t点单日期\t\t\t\t\t状态");
        for (Bill b : allBill) {
            System.out.println(b);
        }
    }

    public void showAllBill2() {
        List<MultiTableBean> ms = multiTableBeanService.getAll();
        System.out.println("编号\t\t桌号\t\t菜品名\t\t价格\t\t\t数量\t\t合计\t\t\t点单日期\t\t\t\t\t\t状态");
        for (MultiTableBean b : ms) {
            Integer id = b.getId();
            Integer diningTableId = b.getDiningTableId();
            String name = b.getName();
            Double price = b.getPrice();
            Integer nums = b.getNums();
            Double money = b.getMoney();
            Date billDate = b.getBillDate();
            String state = b.getState();
            System.out.println(id +
                    "\t\t" + diningTableId +
                    "\t\t" + name +
                    "\t\t" + price +
                    "\t\t" + nums +
                    "\t\t" + money +
                    "\t\t" + billDate +
                    "\t\t" + state);
        }
    }

    public void order() {
        System.out.println("==============点餐服务==============");
        System.out.println("请选择要点餐的桌号(0退出)：");
        int diningTableId = Utility.readInt();
        if (diningTableId == 0) {
            System.out.println("已退出点餐服务...");
            return;
        }
        //检查该餐桌状态
        DiningTable diningTable = diningTableService.getDingById(diningTableId);
        if (diningTable == null) {
            System.out.println("该餐桌不存在！");
            return;
        }
        /*if (!diningTable.getState().equals("空")) {
            System.out.println("该餐桌已被预定或用餐中！");
            return;
        }*/ //预定可以点餐 用餐中点餐相当于加菜
        System.out.println("请选择要菜品编号(0退出)：");
        int menuId = Utility.readInt();
        if (menuId == 0) {
            System.out.println("已退出点餐服务...");
            return;
        }
        Menu menu = menuService.getMenuById(menuId);
        if (menu == null) {
            System.out.println("该菜品不存在");
            return;
        }
        System.out.println("请选择要菜品数量(0退出)：");
        int nums = Utility.readInt();
        if (nums <= 0) {
            System.out.println("已退出点餐服务...");
            return;
        }
        if (billService.addBill(menuId, nums, diningTableId)) {
            System.out.println("点餐成功！");
        } else {
            System.out.println("点餐失败！");
        }
    }

    public void showMenu() {
        List<Menu> all = menuService.getAll();
        System.out.println("编号\t\t菜名\t\t\t\t类型\t\t价格");
        for (Menu m : all) {
            System.out.println(m);
        }
    }

    public void orderDiningTable() {
        System.out.println("=================预定=================");
        System.out.print("请输入要预定的餐桌号(0退出)：");
        int id = Utility.readInt();
        if (id != 0) {
            DiningTable diningTable = diningTableService.getDingById(id);
            if (diningTable == null) {
                System.out.println("不存在该餐桌！");
                return;
            }
            //餐桌状态不为 "空" 不能预定
            if (!("空".equals(diningTable.getState()))) {
                System.out.println("该餐桌已被预定或在用餐中，不能预定！");
                return;
            }
            //可以预定该餐桌
            System.out.print("请输入预定人姓名：");
            String orderName = Utility.readString(30);
            System.out.print("请输入预定人联系电话：");
            String orderTel = Utility.readString(30);
            if (diningTableService.orderDingTable(id, orderName, orderTel)) {
                System.out.println("预定成功！");
            } else {
                System.out.println("预定失败！");
            }
        } else {
            System.out.println("已取消预定...");
        }
    }

    public void showAllDiningTable() {
        List<DiningTable> allDins = diningTableService.getAllDins();
        System.out.println("餐桌号\t状态\t\t\t预定人\t\t联系方式");
        for (DiningTable d : allDins) {
            System.out.println(d);
        }
    }

    public void mainMenu() {
        while (loop) {
            System.out.println("==================满汉楼==================");
            System.out.println("\t\t1 登录满汉楼");
            System.out.println("\t\t2 退出满汉楼");
            System.out.print("请输入你的选择：");
            input = Utility.readString(1);
            switch (input) {
                case "1":
                    System.out.println("请输入员工号：");
                    String id = Utility.readString(20);
                    System.out.println("请输入密码：");
                    String pwd = Utility.readString(20);
                    //访问数据库进行用户判断
                    Employee employee = employeeService.getEmpByIdAndPwd(id, pwd);
                    if (employee != null) {
                        System.out.println("登录成功！欢迎使用满汉楼！");
                        secondMenu(employee.getName());
                    } else {
                        System.out.println("登录失败！用户名或密码错误！");
                    }
                    break;
                case "2":
                    System.out.println("退出满汉楼");
                    loop = false;
                    break;
                default:
                    System.out.println("暂无该选项！");
                    break;
            }
        }
        System.out.println("退出满汉楼...");
    }

    public void secondMenu(String name) {
        while (loop) {
            System.out.println("============满汉楼二级菜单(" + name + ")============");
            System.out.println("\t\t 1 显示餐桌状态");
            System.out.println("\t\t 2 预定餐桌");
            System.out.println("\t\t 3 显示所有菜品");
            System.out.println("\t\t 4 点餐服务");
            System.out.println("\t\t 5 查看账单");
            System.out.println("\t\t 6 结账");
            System.out.println("\t\t 0 退出满汉楼");
            System.out.print("请输入你的选择：");
            input = Utility.readString(1);
            switch (input) {
                case "1":
                    //System.out.println("显示餐桌状态");
                    showAllDiningTable();
                    break;
                case "2":
                    //System.out.println("预定餐桌");
                    orderDiningTable();
                    break;
                case "3":
                    //System.out.println("显示所有菜品");
                    showMenu();
                    break;
                case "4":
                    //System.out.println("点餐服务");
                    order();
                    break;
                case "5":
                    //System.out.println("查看账单");
                    showAllBill2();
                    break;
                case "6":
                    //System.out.println("结账");
                    checkout();
                    break;
                case "0":
                    loop = false;
                    break;
                default:
                    System.out.println("暂无该选项！");
                    break;
            }
        }
    }
}
